package org.jeecg.modules.param.fault.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.constant.BikeConstant;
import org.jeecg.modules.param.fault.entity.BikeFaultWarn;
import org.jeecg.modules.param.fault.mapper.BikeFaultWarnMapper;
import org.jeecg.modules.param.fault.service.IBikeFaultWarnService;
import org.jeecg.modules.param.fault.vo.BikeFaultWarnRes;
import org.jeecg.modules.param.sysparam.entity.BikeSysParam;
import org.jeecg.modules.param.sysparam.mapper.BikeSysParamMapper;
import org.jeecg.modules.utils.ExcelUtils;
import org.jeecg.modules.utils.RandomCodeUtils;
import org.jeecg.modules.work.dispatch.entity.BikeWorkDispatch;
import org.jeecg.modules.work.dispatch.mapper.BikeWorkDispatchMapper;
import org.jeecg.modules.work.repair.entity.BikeWorkRepair;
import org.jeecg.modules.work.repair.mapper.BikeWorkRepairMapper;
import org.jeecg.modules.work.sweep.entity.BikeWorkSweep;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Description: 设备预警消息
 * @Author: 月月鸟
 * @Date:   2021-06-03
 * @Version: V1.0
 */
@Service
public class BikeFaultWarnServiceImpl extends ServiceImpl<BikeFaultWarnMapper, BikeFaultWarn> implements IBikeFaultWarnService {
    @Resource
    private BikeFaultWarnMapper bikeFaultWarnMapper;

    @Resource
    private BikeSysParamMapper bikeSysParamMapper;
    @Resource
    private BikeWorkRepairMapper bikeWorkRepairMapper;
    @Resource
    private BikeWorkDispatchMapper bikeWorkDispatchMapper;
    @Override
    public Result<?> pageList(Integer pageNo, Integer pageSize, BikeFaultWarn bikeFaultWarn) {
        List<BikeFaultWarn> bikeFaultWarns = bikeFaultWarnMapper.pageList(bikeFaultWarn);
        bikeFaultWarns.sort(new Comparator<BikeFaultWarn>() {
            @Override
            public int compare(BikeFaultWarn o1, BikeFaultWarn o2) {
                return o2.getCreateTime().compareTo(o1.getCreateTime());
            }
        });
        return Result.OK(bikeFaultWarns);
    }

    @Override
    public ModelAndView exportXls(HttpServletRequest request, BikeFaultWarn bikeFaultWarn) {
        List<BikeFaultWarn> bikeFaultWarns = bikeFaultWarnMapper.pageList(bikeFaultWarn);
        return ExcelUtils.exportXls(request,bikeFaultWarns,BikeFaultWarn.class,"预警消息查询");
    }

    @Override
    public Result<?> goWorkOrder(BikeFaultWarnRes bikeFaultWarn) {
        QueryWrapper<BikeSysParam> wrapper = new QueryWrapper<>();

        BikeFaultWarn faultWarnData = bikeFaultWarnMapper.pageListById(bikeFaultWarn.getId());
        wrapper.eq("memo",faultWarnData.getWarnType());
        BikeSysParam bikeSysParam = bikeSysParamMapper.selectOne(wrapper);
        if (faultWarnData.getIsDispatch().equals("1")){
            return Result.error("已转工单");
        }
        //转调度
        if (bikeSysParam.getId().equals("100001")||bikeSysParam.getId().equals("100002")){
            BikeWorkDispatch bikeWorkDispatch = new BikeWorkDispatch();
            //编号
            bikeWorkDispatch.setCode(RandomCodeUtils.getRandomCode(BikeConstant.work_dispatch_open));
            //任务描述
            bikeWorkDispatch.setTaskDescribe(bikeFaultWarn.getContent());
            //是否传照片 1 是 0 否
            bikeWorkDispatch.setIsPicture(bikeFaultWarn.getIsPicture());
            //负责人
            // 负责人从后台用户获取
            bikeWorkDispatch.setUserId(bikeFaultWarn.getUserDutyId());
            //截止完成时间
            bikeWorkDispatch.setFinishTime(bikeFaultWarn.getFinishTime());
            //紧急程度 2 一般 1加急
            bikeWorkDispatch.setUrgentType(bikeFaultWarn.getUrgentType());
            //是否派单  1 是  0 否
            bikeWorkDispatch.setIsDispatch(bikeFaultWarn.getIsDispatch());
            bikeWorkDispatchMapper.insert(bikeWorkDispatch);
            //成功后
            BikeFaultWarn faultWarn = new BikeFaultWarn();
            faultWarn.setId(bikeFaultWarn.getId());
            //是否转工单  1 是 2 否
            faultWarn.setIsDispatch(BikeConstant.y_n_yes);
            bikeFaultWarnMapper.updateById(faultWarn);
            return Result.OK("转工单成功");
        }
        //转维修
        if (bikeSysParam.getId().equals("100003")||bikeSysParam.getId().equals("100004")||bikeSysParam.getId().equals("100005")){
            BikeWorkRepair bikeWorkRepair = new BikeWorkRepair();
            //单号
            bikeWorkRepair.setCode(RandomCodeUtils.getRandomCode(BikeConstant.work_repair_open));
            //网点编号
            bikeWorkRepair.setStationCode(faultWarnData.getStationCode());
            //网点名
            bikeWorkRepair.setStationName(faultWarnData.getStationName());
            //车桩编号
            bikeWorkRepair.setDeviceCode(faultWarnData.getPileCode());
            //网点区域
            bikeWorkRepair.setAddress(faultWarnData.getStationArea());
            //故障类型(1还不了车2租不了车3网络故障4车辆损坏5车锁故障6需要保洁7其他问题)
            bikeWorkRepair.setRepairItem("7");
            //是否必须上传照片 1是 0否
            bikeWorkRepair.setIsPicture(bikeFaultWarn.getIsPicture());
            //工单来源 (1新增2检测故障3上报故障 4预警消息)
            bikeWorkRepair.setWorkSource("4");
            // 负责人从后台用户获取
            bikeWorkRepair.setUserId(bikeFaultWarn.getUserDutyId());
            //截止时间
            bikeWorkRepair.setFinishTime(bikeFaultWarn.getFinishTime());
            //是否派单 1是 0否
            bikeWorkRepair.setIsDispatch(bikeFaultWarn.getIsDispatch());
            //紧急程度 2 一般 1加急
            bikeWorkRepair.setUrgentType(bikeFaultWarn.getUrgentType());
            //备注
            bikeWorkRepair.setMemo(faultWarnData.getWarnType());
            bikeWorkRepairMapper.insert(bikeWorkRepair);
            //成功后
            BikeFaultWarn faultWarn = new BikeFaultWarn();
            faultWarn.setId(bikeFaultWarn.getId());
            //是否转工单  1 是 2 否
            faultWarn.setIsDispatch(BikeConstant.y_n_yes);
            bikeFaultWarnMapper.updateById(faultWarn);
            return Result.OK("转工单成功");
        }
        return Result.error("转工单失败");
    }
}
